from selenium import webdriver
from time import sleep
from  selenium.webdriver.common.by import By
from A3mall.Sql_tools import Mysql_tools
from selenium.webdriver.common.keys import Keys
import unittest






class Test_a3mall_maotai(unittest.TestCase):
    def setUp(self) -> None:
        self.db = Mysql_tools('root', 'yidong666', 3306, 'a3mall', '10.59.9.12')
        # 浏览器驱动
        self.driver = webdriver.Chrome()
        self.driver.get('http://10.59.9.12:7001/')
        sleep(2)
        self.Text1=0

    def test_01_login(self):
        self.driver.find_element(By.LINK_TEXT, '登录').click()
        sleep(2)
        # 做登录 输入账号密码
        self.driver.find_element(By.NAME, 'username').send_keys('15523670956')
        self.driver.find_element(By.NAME, 'password').send_keys('123456Aa')
        sleep(7)
        self.driver.find_element(By.CSS_SELECTOR, '.finish-btn').click()
        sleep(5)

        Text = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[1]').text
        self.Text1 = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[4]/span').text
        # 转为浮点数
        self.Text1 = float(self.Text1.replace('¥ ', ''))
        print('账户余额为:', self.Text1)
        # 查询数据库余额
        money = (self.db.sql_operation('select amount from mall_users where username="15523670956"'))[0][0]
        message = (self.db.sql_operation('select amount from mall_users where username="15523670956"'))
        print(message)
        print('数据库余额为:', money)
        # 登录成功断言余额 欢迎语
        self.assertEqual(self.Text1,money)
        self.assertIn('15523670956',Text)


    def test_02_buy(self):
        self.test_01_login()
        search = self.driver.find_element(By.CLASS_NAME, 'search-input')
        search.send_keys('茅台')
        search.send_keys(Keys.ENTER)
        maotai = self.driver.find_element(By.LINK_TEXT, '茅台--传世经典,百年国酒')
        sp_name = maotai.text
        maotai.click()
        print(sp_name)

        db_price = (self.db.sql_operation(f'select sell_price from mall_goods where title="{sp_name}"'))[0][0]
        print("数据库商品价格:", db_price)
        sp_price = float(self.driver.find_element(By.XPATH, '//*[@id="sell-price-box"]').text)
        print("页面商品价格",sp_price)
        # 断言商品价格
        self.assertEqual(sp_price,db_price)
        self.driver.find_element(By.ID,'buy-cart').click()
        # 商品数量
        sp_num=int(self.driver.find_element(By.XPATH,'//*[@id="theForm"]/div[4]/div[1]/table/tbody/tr[2]/td[3]').text)
        print('商品数量',sp_num)
        sp_money=float(self.driver.find_element(By.XPATH,'//*[@id="order-amount"]').text)
        print('商品总价:',sp_money)
        self.assertEqual(sp_num*sp_price+1,sp_money)

        self.driver.find_element(By.ID,'order-button').click()
        sleep(2)
        # 获取订单号
        Text_id=self.driver.find_element(By.XPATH,'/html/body/div[4]/div[4]/div[2]/ul/li[1]').text
        Text_id=Text_id.replace('订单编号：','')
        print('订单编号', Text_id)
        # 点击查看订单
        self.driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/p/a').click()
        # 再次获取订单号
        get_id=self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[2]/td[2]').text
        print('订单编号',get_id)
        self.assertEqual(Text_id,get_id)
        # 刷新页面
        self.driver.refresh()
        db = Mysql_tools('root', 'yidong666', 3306, 'a3mall', '10.59.9.12')
        money_two = (db.sql_operation('select amount from mall_users where username="15523670956"'))[0][0]
        self.assertEqual(money_two,self.Text1-sp_money)
        print(money_two)
        print(self.Text1-sp_money)
        db.sql_close()










    def tearDown(self) -> None:
        sleep(10)
        self.driver.quit()
        self.db.sql_close()
if __name__ == '__main__':
    unittest.main()

# # 进入秒杀模块
# driver.find_element(By.LINK_TEXT,'秒杀').click()
# sleep(2)
# # 点击要买的商品
# driver.find_element(By.CSS_SELECTOR,'.lazy').click()
# sleep(1)
# Text_price=float(driver.find_element(By.XPATH,'//*[@id="sell-price-box"]').text)
# # 点击立即购买
# driver.find_element(By.CSS_SELECTOR,'.shop-btn').click()
# # 选择收货地址
# driver.find_element(By.XPATH,'//*[@id="address-77"]').click()
# # 获取商品数量
# Text_num=int(driver.find_element(By.XPATH,'//*[@id="theForm"]/div[4]/div[1]/table/tbody/tr[2]/td[3]').text)
# # 点击下一步
# driver.find_element(By.XPATH,'//*[@id="order-button"]').click()
# sleep(3)

# Text_id=Text_id.replace('订单编号：','')
# print('订单编号是:',Text_id)
#
# # 获取购买成功断言
# success=driver.find_element(By.XPATH,'/html/body/div[4]/div[3]/span').text
# print(success)
# assert '您的订单支付成功'==success,('未购买成功')
# # 点击查看订单
# driver.find_element(By.XPATH,'/html/body/div[4]/div[3]/p/a').click()
#

# assert Text_id == get_id,'订单编号有误'









































